home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / us20src.zip / CDICT.C < prev    next >
C/C++ Source or Header  |  1992-06-26  |  5KB  |  218 lines

  1. /*    CDICT:    Compress Dictionary utility program for
  2.         MicroSPELL 2.0
  3.  
  4.         (C)opyright May 1987,1992 by Daniel Lawrence
  5.         All Rights Reserved
  6. */
  7.  
  8. #include    <stdio.h>
  9. #include    "dopt.h"
  10. #include    "dsfx.h"
  11.  
  12. /* globals */
  13.  
  14. char mdfile[NSTRING];        /* main dictionary text file name */
  15. char mcfile[NSTRING];        /* compressed dictionary file name */
  16. FILE *mdptr = NULL;        /* main dictionary file pointer */
  17. FILE *mcptr = NULL;        /* compressed dictionary file pointer */
  18. int sflen[NSUFFIX];        /* length of suffixes */
  19. long letter_offset[ALPHASIZE];    /* offsets in file to letters */
  20. unsigned char lcase[128];    /* lower case table (quick!) */
  21.  
  22. main(argc, argv)
  23.  
  24. int argc;    /* # of command line arguments */
  25. char **argv;    /* text of command line arguments */
  26.  
  27. {
  28.     register char *word;        /* current word */
  29.     register int suffix;        /* suffix index */
  30.     register char cur_first_letter;    /* current first letter scanned */
  31.     register int index;        /* loop index */
  32.     long total_words;        /* total number of words in this dictionary */
  33.     char lastword[NSTRING];        /* previous word in dictionary */
  34.     char tempword[NSTRING];        /* temporary word in dictionary */
  35.     char *nxtmword();
  36.  
  37.     printf("CDICT Dictionary Compression Utility for MicroSPELL v%s\n",
  38.         VERSION);
  39.  
  40.     if (argc != 3) {
  41.         help();
  42.         exit(EXBADOPT);
  43.     }
  44.  
  45.     strcpy(mdfile, argv[1]);
  46.     strcpy(mcfile, argv[2]);
  47.  
  48.     if (mopen() != TRUE) {
  49.         printf("%%Can not open text dictionary file\n");
  50.         exit(EXMDICT);
  51.     }
  52.  
  53.     /* init the lower case table */
  54.     for (index = 0; index < 128; index ++)
  55.         if ('A' <= index && index <= 'Z')
  56.             lcase[index] = index - 'A' + 'a';
  57.         else
  58.             lcase[index] = index;
  59.  
  60.     /* open the output compressed dictionary file */
  61.     mcptr = fopen(mcfile, "wb");
  62.     if (mcptr == NULL) {
  63.         printf("%%Can not open compressed dictionary output file\n");
  64.         exit(EXMDICT);
  65.     }
  66.  
  67.     /* position past character table */
  68.     fwrite((char *)&(letter_offset[0]), sizeof(long), ALPHASIZE, mcptr);
  69.  
  70.     /* prepare the suffix length table */
  71.     for (suffix = 0; suffix < NSUFFIX; suffix++)
  72.         sflen[suffix] = strlen(sfx[suffix]);
  73.  
  74.     printf("[Compressing %s => %s]\n", mdfile, mcfile);
  75.     lastword[0] = 0;    /* null last word */
  76.  
  77.     /* scan the dictionary, compressing */
  78.     cur_first_letter = 0;
  79.     total_words = 0L;
  80.     word = nxtmword();
  81.     while (word) {
  82.         if (lcase[word[0]] != cur_first_letter) {
  83.             cur_first_letter = lcase[word[0]];
  84.             letter_offset[cur_first_letter - 'a'] = ftell(mcptr);
  85.         }
  86.         strcpy(tempword, word);
  87.         cmpsword(lastword, word);
  88.         total_words++;
  89.         strcpy(lastword, tempword);
  90.         word = nxtmword();
  91.     }
  92.  
  93.     /* write out letter offset table to front of file */
  94.     fseek(mcptr, 0, 0);
  95.     fwrite((char *)&(letter_offset[0]), sizeof(long), ALPHASIZE, mcptr);
  96.  
  97.     /* close things up */
  98.     mclose();
  99.     fclose(mcptr);
  100.     printf("[%ld words in dictionary compressed]\n", total_words);
  101. }
  102.  
  103. help()        /* tell us about cdict... */
  104.  
  105. {
  106.     printf("\nUsage:\n\n");
  107.     printf("    CDICT <text dictionary> <compressed dictionary>\n");
  108. }
  109.  
  110. mopen()        /* open the main dicionary */
  111.  
  112. {
  113.     /* if it is already open, close it down */
  114.     if (mdptr != NULL)
  115.         fclose(mdptr);
  116.  
  117.     /* open up the text dictionary... */
  118.     if ((mdptr = fopen(mdfile, "r")) == NULL)
  119.         return(FALSE);
  120.  
  121.     return(TRUE);
  122. }
  123.  
  124. mclose()    /* close the dictionary down */
  125.  
  126. {
  127.     /* if it is already open, close it down */
  128.     if (mdptr != NULL)
  129.         fclose(mdptr);
  130.     mdptr = NULL;
  131. }
  132.  
  133. char *nxtmword()    /* get the next word from the main dictionary */
  134.  
  135. {
  136.     static char word[NSTRING];    /* word to return */
  137.  
  138.     /* is it already closed? */
  139.     if (mdptr == NULL)
  140.         return(NULL);
  141.  
  142.     /* get the next word */
  143.     if (fgets(word, NSTRING - 1, mdptr) == NULL) {
  144.         /* no more left!!!! close out */
  145.         fclose(mdptr);
  146.         mdptr = NULL;
  147.         return(NULL);
  148.     }
  149.  
  150.     /* all's well, dump the return, any trailing spaces and
  151.        return the word */
  152.     do
  153.         word[strlen(word) - 1] = 0;
  154.     while (word[strlen(word) - 1] == ' ');
  155.     return(word);
  156. }
  157.  
  158. cmpsword(lastword, word)    /* compress the given word */
  159.  
  160. char *lastword;        /* previous dictionary word */
  161. char *word;        /* current dictionary word */
  162.  
  163. {
  164.     register int index;    /* index into current word */
  165.     register int same;    /* # of same characters */
  166.     register int suffix;    /* suffix code */
  167.     register int wlen;    /* length of current word */
  168.     register int orig_first;    /* original first character */
  169.  
  170.     /* scan for common suffixes */
  171.     wlen = strlen(word);
  172.     for (suffix = 0; suffix < NSUFFIX; suffix++) {
  173.         if (wlen < sflen[suffix])
  174.             continue;
  175.         if (strcmp(&word[wlen - sflen[suffix]], sfx[suffix]) == 0) {
  176.             word[wlen - sflen[suffix]] = 0;    /* trunc it */
  177.             break;
  178.         }
  179.     }
  180.  
  181.     /* If there is no suffix...suffix ends up as NSUFFIX */
  182.  
  183.     /* save the capitalization of the original lead char */
  184.     word[0] = lcase[(orig_first = word[0])];
  185.  
  186.     /* scan for like beginning characters */
  187.     index = 0;
  188.     while (lastword[index] && lastword[index] == word[index])
  189.         index++;
  190.  
  191.     same = index;
  192. #if    ASCII
  193.     suffix |= 128;
  194. #endif
  195.  
  196.     if (orig_first == word[0])
  197.         fprintf(mcptr, "%c%s%c", 'A'+same, &word[index], suffix);
  198.     else
  199.         fprintf(mcptr, "%c%s%c", 'A'+same+128, &word[index], suffix);
  200. }
  201.  
  202. #if    CMS
  203. #undef    fopen
  204. /*    The IBM 30xx likes to tell us when file opens
  205.     fail...it's too chatty....I like to handle these myself    */
  206.  
  207. FILE *cmsopen(file, mode)
  208.  
  209. char *file;    /* name of file to open */
  210. char *mode;    /* mode to open it in */
  211.  
  212. {
  213.     quiet(1);
  214.     return(fopen(file,mode));
  215. }
  216. #endif
  217.  
  218.